<template>
<el-table 
    :data="dataMap.finalTableData" style="width: 100% !important;height:100% !important;margin-top:5px;"
    @selection-change="handleSelectionChange"
    >
    <el-table-column type="selection" width="55" />
    <template v-for="(item,key) in filters">
        <el-table-column :prop="key" :label="item.title" v-if="item.is_list == 1" />
    </template>
  </el-table>
</template>
<script setup>
import {reactive,onMounted,watch} from 'vue'
const emit = defineEmits(['getSelections']);

let dataMap = reactive({
    finalTableData:[],
});
const props = defineProps({
    filters:{
      type:Array,
      default:()=>{
        return [];
      }
    },
    tableData:{
      type:Object,
      default:()=>{
        return {};
      }
    },
})

const handleSelectionChange = (selections)=>{
    emit('getSelections',selections);
}

watch([()=>props.tableData],(newVal)=>{
    randerTableData(newVal[0]);
})

const randerTableData = (data)=>{
    dataMap.finalTableData = [];
    //处理一下table数据
    for(let i in data){
        let tempArr = {};
        for(let j in props.filters){
            if(props.filters[j].is_list == 1){
                if(props.filters[j].alias == ''){
                    //判断是否是关联显示的，即list_data 不是空数组
                   if(Object.keys(props.filters[j].list_data).length == 0){
                       tempArr[j] = data[i][j]
                   }else{
                       tempArr[j] = props.filters[j].listData[data[i][j]]
                   }
                }else{
                    if(data[i][props.filters[j].alias] != null){
                        tempArr[j] = data[i][props.filters[j].alias][props.filters[j].actual_field];
                    }else{
                        tempArr[j] = '';
                    }
                }
                //判断是否有关联输出
                if(props.filters[j].required_field.length > 0){
                    for(let k in props.filters[j].required_field){
                        if(props.filters[j].alias == ''){
                            tempArr[props.filters[j].required_field[k]] = data[i][props.filters[j].required_field[k]];
                        }else{
                            if(data[i][props.filters[j].alias] != null){
                                let tempKey = props.filters[j].alias+'_'+props.filters[j].required_field[k];
                                tempArr[tempKey] = data[i][props.filters[j].alias][props.filters[j].required_field[k]];
                            }else{
                                tempArr[props.filters[j].required_field[k]] = '';
                            }
                        }
                    }
                }
            }
        }
        dataMap.finalTableData.push(tempArr);
    }
}
</script>
<style leng="scss" scoped>

</style>